var async = require('async');
var Model = require('../../model/core/users');
var Department = require('../../model/core/department');
var Role = require('../../model/core/role');

var crypto = require('../../util/crypto');
var data = require('../../util/result');
var session = require('express-session');

var index = {page:'core/users',title:''};
var form = {page:'core/_form/_users_form',title:'用户信息管理'};

exports.login = function (req,res) {
    var model = new Model();
    var obj = req.body;
    model.findByUserName(obj.userName,function(err,result){
        if(err){
            res.json(data.result(1,"数据异常"));
        }
        if((typeof result) == 'object'){
            var salt = result.salt;
            var pwd = crypto.md5(obj.password+salt);
            //密码验证
            if(pwd != result.password){
                res.json(data.result(1,"用户名或者密码有误"));
                return;
            }
            //状态验证
            if(result.status != 1){
                res.json(data.result(1,"当前账号被禁用"));
                return;
            }
            //去掉不需要的返回的参数
            delete result["salt"];
            delete result["password"];
            //放入session
            req.session.user = result;
            //返回到html中
            res.json(data.result(0,"ok",result));
        }else{
            res.json(data.result(1,"用户名或者密码有误"));
        }
    });
}

exports.index = function (req,res) {
    res.render(index.page, {title: index.title});
};

exports.form = function (req,res) {
    var model = new Model();
    var department = new Department();
    var role = new Role();
    var cond = {where:'',order:' id desc'};
    async.waterfall([
        function(callback) {
            role.all(cond,function (err,result) {
                callback(null,result);
            });
        },
        function(vals, callback) {
            var data = {role:vals};
            department.all(cond,function (err,results) {
                data.department = results;
                callback(null,data);
            })
        },
        function(vals, callback) {
            var data = {};
            model.one(req.params.id,function (err,result) {
                if(err){
                    data ={
                        title: form.title,
                        department:vals.department,
                        role:vals.role.data
                    }
                    callback(null,data);
                }else {
                    data ={
                        title: form.title,
                        department:vals.department,
                        role:vals.role.data,
                        obj:result
                    }
                    callback(null,data);
                }
            })
        }
    ], function(error, result) {
        res.render(form.page, result);
    });
};

exports.one = function (req,res) {
    var model = new Model();
    model.one(req.params.id,function (err,result) {
        if(err){
            res.json(data.result(1,"数据异常"));
            return;
        }
        if((typeof result) == 'object'){
            res.json(data.result(0,"",result));
        }else{
            res.json(data.result(1,"数据异常"));
        }
    })
}

exports.insert = function (req,res) {
    var model = new Model();
    var json = req.body;
    var where = " and userName='"+json.userName+"' ";

    async.waterfall([
        function(callback) {
            model.checkUserName(where,function (err,result) {
                callback(null,result);
            });
        },
        function(check, callback) {
            if((typeof check) == 'object'){
                callback(null,data.result(1,"登录名已经存在"));
            }else {
                var salt = (Math.random()+"").substr(2,6);
                var pwd = crypto.md5(json.password+salt);
                json.password = pwd;
                json.salt = salt;
                model.insert(json,function (err,result) {
                    if(err){
                        callback(null,data.result(1,"数据异常"));
                    }
                    if(result.insertId > 0){
                        callback(null,data.result(0,"数据操作成功"));
                    }else{
                        callback(null,data.result(1,"数据异常"));
                    }
                });
            }
        }
    ], function(error, result) {
        res.json(result);
    });
}

exports.edit = function (req,res) {
    var model = new Model();
    var json = req.body;
    console.log('body',json);
    var id = req.params.id;
    var where = " and userName='"+json.userName+"' and id<>"+id+" ";
    json.id = id;
    async.waterfall([
        function(callback) {
            model.checkUserName(where,function (err,result) {
                callback(null,result);
            });
        },
        function(check, callback) {
            if((typeof check) == 'object'){
                callback(null,data.result(1,"登录名已经存在"));
            }else {
                model.edit(json,function (err,result) {
                    if(err){
                        callback(null,data.result(1,"数据异常"));
                        return;
                    }
                    if(result.affectedRows > 0){
                        callback(null,data.result(0,"数据操作成功"));
                    }else{
                        callback(null,data.result(1,"数据异常"));
                    }
                });
            }
        }
    ], function(error, result) {
        res.json(result);
    });
}

exports.del = function (req,res) {
    var model = new Model();
    req.body.id = req.params.id;
    model.del(req.body,function (err,result) {
        if(err){
            res.json(data.result(1,"数据异常"));
            return;
        }
        if(result.affectedRows > 0){
            res.json(data.result(0,"数据操作成功"));
        }else{
            res.json(data.result(1,"数据异常"));
        }
    });
}

exports.list = function (req,res) {
    var model = new Model();
    var where = req.body;
    model.list(where,function (err,result) {
        if(err){
            res.json(data.result(1,"数据异常"));
            return;
        }
        if((typeof result) == 'object'){
            res.json(result);
        }else{
            res.json(data.result(1,"数据异常"));
        }
    });
}

exports.init = function (req,res) {
    var model = new Model();
    var salt = (Math.random()+"").substr(2,6);
    var pwd = crypto.md5('111111'+salt);
    var json = {password:pwd,salt:salt,id:req.params.id};
    model.init(json,function (err,result) {
        if(err){
            res.json(data.result(1,"数据异常"));
            return;
        }
        if(result.affectedRows > 0){
            res.json(data.result(0,"数据操作成功"));
        }else{
            res.json(data.result(1,"数据异常"));
        }
    });
}



